<!-- src/App.vue -->
<template>
  <div id="app">
    <router-view />
    
    <!-- 登录提示弹窗 -->
    <div v-if="showLoginModal" class="login-modal-overlay">
      <div class="login-modal">
        <div class="login-modal-header">
          <h3>提示</h3>
        </div>
        <div class="login-modal-body">
          <p>您尚未登录，请先登录后再访问该页面</p>
        </div>
        <div class="login-modal-footer">
          <button @click="cancelLogin" class="cancel-btn">取消</button>
          <button @click="goToLogin" class="confirm-btn">去登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      showLoginModal: false,
      targetPath: null
    }
  },
  mounted() {
    // 监听需要登录的事件
    window.addEventListener('need-login', this.handleNeedLogin)
  },
  methods: {
    handleNeedLogin(event) {
      this.targetPath = event.detail?.path || null
      this.showLoginModal = true
    },
    goToLogin() {
      this.showLoginModal = false
      this.$router.push({
        path: '/login',
        query: this.targetPath ? { redirect: this.targetPath } : {}
      })
    },
    cancelLogin() {
      this.showLoginModal = false
      // 如果用户取消登录，返回上一页或者首页
      if (this.$router.currentRoute.value.path !== '/') {
        this.$router.go(-1)
      }
    }
  }
}
</script>

<style>
.login-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.login-modal {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  width: 400px;
  max-width: 90%;
}

.login-modal-header {
  padding: 20px 20px 10px;
  border-bottom: 1px solid #eee;
}

.login-modal-header h3 {
  margin: 0;
  color: #333;
}

.login-modal-body {
  padding: 20px;
  text-align: center;
}

.login-modal-body p {
  margin: 0;
  color: #666;
  font-size: 16px;
}

.login-modal-footer {
  padding: 20px;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  border-top: 1px solid #eee;
}

.cancel-btn, .confirm-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.cancel-btn {
  background-color: #f5f5f5;
  color: #333;
}

.cancel-btn:hover {
  background-color: #e0e0e0;
}

.confirm-btn {
  background-color: #4caf50;
  color: white;
}

.confirm-btn:hover {
  background-color: #43a047;
}
</style>